package com.heima.datastructure.linkedlist;
import java.util.Iterator;
import java.util.function.Consumer;

public class DoublyLinkedListSentinel implements Iterable<Integer> {
    private final Node head; // 头哨兵
    private final Node tail; // 尾哨兵

    /**
     * 用无参构造初始化链表
     */
    public DoublyLinkedListSentinel() {
        this.head = new Node(null, 666, null);
        this.tail = new Node(null, 888, null);

        head.next = tail;
        tail.prev = head;
    }

    /**
     * 节点类
     */
    private static class Node {
        Node prev; // 上一个节点
        int value; // 值
        Node next; // 下一个节点

        public Node(Node prev, int value, Node next) {
            this.prev = prev;
            this.value = value;
            this.next = next;
        }
    }

    /**
     * 头插法
     *
     * @param value 插入节点的值
     */
    public void addFirst(int value) {
        insert(0, value);
    }

    /**
     * 尾插法
     * @param value 插入节点的值
     */
    public void addLast(int value){
        Node last = tail.prev;
        Node added = new Node(last, value, tail);
        last.next = added;
        tail.prev = added;
    }

    /**
     * 遍历链表1
     * @param consumer 消费者
     */
    public void loop1(Consumer<Integer> consumer){
        Node p = head.next;
        while (p != tail){
            consumer.accept(p.value);
            p = p.next;
        }
    }

    /**
     * 遍历链表2
     * @param consumer 消费者
     */
    public void loop2(Consumer<Integer> consumer){
        for (Node p = head.next; p != tail; p = p.next){
            consumer.accept(p.value);
        }
    }

    /**
     * 递归遍历链表
     * @param before 递的时候要做的事情
     * @param after  归的时候要做的事情
     */
    public void loop3(Consumer<Integer> before, Consumer<Integer> after){
        recursion(head.next,before,after);
    }

    /**
     * 递归遍历链表
     * @param curr 当前节点
     * @param before 递的时候要做的事情
     * @param after  归的时候要做的事情
     */
    private void recursion(Node curr, Consumer<Integer> before, Consumer<Integer> after){
        if (curr == tail){
            return;
        }
        before.accept(curr.value);
        recursion(curr.next,before,after);
        after.accept(curr.value);
    }

    /**
     * 遍历链表3
     * @return 迭代器
     */
    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node p = head.next;

            @Override
            public boolean hasNext() {
                return p != tail;
            }

            @Override
            public Integer next() {
                int value = p.value;
                p = p.next;
                return value;
            }
        };
    }

    /**
     * 抛出index非法异常
     *
     * @param index 非法的索引
     * @return 参数非法异常
     */
    private IllegalArgumentException illegalIndex(int index) {
        return new IllegalArgumentException(
                String.format("index [%d] 不合法%n", index));
    }

    /**
     * 找指定位置的节点
     *
     * @param index 指定位置的索引
     * @return 找到的节点
     */
    private Node findNode(int index) {
        int i = -1;
        for (Node p = head; p != tail; p = p.next, i++) {
            if (index == i) {
                return p;
            }
        }
        return null;
    }

    /**
     * 指定位置插入节点
     *
     * @param index 指定位置的索引
     * @param value 插入节点的值
     */
    public void insert(int index, int value) {
        Node prev = findNode(index - 1);
        if (prev == null) {
            throw illegalIndex(index);
        }
        Node next = prev.next;
        Node inserted = new Node(prev, value, next);

        prev.next = inserted;
        next.prev = inserted;
    }

    /**
     * 查找指定位置节点的值
     * @param index 指定位置索引
     * @return 查到节点的值
     */
    public int get(int index){
        Node node = findNode(index);
        if (node == null || node == head){
            throw illegalIndex(index);
        }
        return node.value;
    }

    /**
     * 删除第一个节点
     * @return 返回删除的节点的值
     */
    public int removeFirst(){
        return remove(0);
    }

    /**
     * 删除最后一个节点
     * @return 删除的节点的值
     */
    public int removeLast(){
        Node removed = tail.prev;
        if (removed == head){
            throw illegalIndex(0);
        }
        Node prev = removed.prev;
        prev.next = tail;
        int value = removed.value;

        tail.prev = prev;
        return value;
    }

    /**
     * 删除指定位置的节点
     *
     * @param index 指定位置的索引
     * @return 删除的节点的值
     */
    public int remove(int index) {
        Node removed = findNode(index);
        if (removed == null || removed == head) {
            throw illegalIndex(index);
        }
        Node prev = removed.prev;
        Node next = removed.next;
        int value = removed.value;

        prev.next = next;
        next.prev = prev;

        return value;
    }
}
